package com.hs.controller;


import com.hs.entity.dto.LoginDTO;
import com.hs.entity.dto.ViewUpdateUserDTO;
import com.hs.entity.pojo.User;
import com.hs.entity.vo.UserHomeVO;
import com.hs.entity.vo.UserInfoVO;
import com.hs.service.UserService;
import com.hs.utils.BeanCopyUtil;
import com.hs.utils.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hs
 * @since 2023-08-05
 */
@Validated
@RestController
@RequestMapping("/user")
@Api(tags = "用户")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/loginByPhone")
    @ApiOperation("手机号一键登录")
    public ResultResponse<UserInfoVO> loginByPhone(@ApiParam("手机号") @RequestParam(name = "phone") String phone){
        return userService.loginByPhone(phone);
    }

    @PostMapping("/login")
    @ApiOperation("用户名密码登录")
    public ResultResponse<UserInfoVO> login(@RequestBody @Validated LoginDTO loginDTO){
        return userService.login(loginDTO);
    }

    @PostMapping("/modifyPassword")
    @ApiOperation("修改密码")
    public ResultResponse<?> modifyPassword(@ApiParam("密码") @RequestParam(name = "password") String password){
        return userService.modifyPassword(password);
    }


    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public ResultResponse<?> logout(){
        return userService.logout();
    }


    @PutMapping("/update")
    @ApiOperation("修改用户信息")
    public ResultResponse<?> updateUserInfo(@RequestBody ViewUpdateUserDTO userDTO){
        userService.updateById(BeanCopyUtil.copyBean(userDTO, User.class));
        return ResultResponse.ok();
    }

    @GetMapping("/home/{userId}")
    @ApiOperation("用户主页信息")
    public ResultResponse<UserHomeVO> getUserHome(@PathVariable Integer userId){
        return userService.getUserHome(userId);
    }


}

